/*
 * Authorize.java
 *
 * Created on July 13, 2006, 11:16 AM
 */

package org.vcboard.view;

import java.awt.event.*;
import javax.swing.*;
import org.vcboard.*;
import org.vcboard.database.MySQLDbIf;
import org.vcboard.util.MD5;

/**
 *
 * @author  Josh
 */
public class Authorize extends javax.swing.JFrame
{
   
   /** Creates new form Authorize */
   public Authorize()
   {
      initComponents();
      
      WorkTimer.centerForm(this);
   }
   
   private void attemptLogin()
   {
      loginButton.requestFocusInWindow();
      
      String username = usernameTextField.getText();
      char[] chars = passwordTextField.getPassword();
      String password = new String(chars);
      
      if (MySQLDbIf.getInstance() != null)
      {
         if (username.length() > 0)
         {
            if (password.length() > 0)
            {
               WorkTimer.setUser(username, new MD5(password).toString());

               if (WorkTimer.getUser() == null)
               {
                  JOptionPane.showMessageDialog(null,
                        "Login failed... Please try again.",
                        "Login failed",
                        JOptionPane.ERROR_MESSAGE);
               }
               else
               {
                  WorkTimer.setAuth(null);
                  dispose();
               }
            }
            else
            {
               JOptionPane.showMessageDialog(null,
                     "You must enter your password!",
                     "Invalid Password",
                     JOptionPane.ERROR_MESSAGE);
            }
         }
         else
         {
            JOptionPane.showMessageDialog(null,
                  "You must enter your username!",
                  "Invalid Username",
                  JOptionPane.ERROR_MESSAGE);
            /*
            usernameTextField.requestFocusInWindow();
            usernameTextField.setSelectionStart(0);
             */
         }
      }
      else
      {
         JOptionPane.showMessageDialog(null,
               "Failed to connect to database.  Please check your connection settings.",
               "Error",
               JOptionPane.ERROR_MESSAGE);
         WorkTimer.getDbInfo().setVisible(true);
      }
   }
   
   private void highlightAllText(JTextField obj)
   {
      obj.setSelectionStart(0);
   }
   
   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
   private void initComponents()
   {
      jLabel1 = new javax.swing.JLabel();
      usernameTextField = new javax.swing.JTextField();
      jLabel2 = new javax.swing.JLabel();
      passwordTextField = new javax.swing.JPasswordField();
      loginButton = new javax.swing.JButton();
      cancelButton = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      setTitle("Login");
      setResizable(false);
      jLabel1.setText("Username:");

      usernameTextField.addFocusListener(new java.awt.event.FocusAdapter()
      {
         public void focusGained(java.awt.event.FocusEvent evt)
         {
            usernameTextFieldFocusGained(evt);
         }
      });
      usernameTextField.addKeyListener(new java.awt.event.KeyAdapter()
      {
         public void keyReleased(java.awt.event.KeyEvent evt)
         {
            usernameTextFieldKeyReleased(evt);
         }
      });

      jLabel2.setText("Password:");

      passwordTextField.setFont(new java.awt.Font("Tahoma", 0, 11));
      passwordTextField.addFocusListener(new java.awt.event.FocusAdapter()
      {
         public void focusGained(java.awt.event.FocusEvent evt)
         {
            passwordTextFieldFocusGained(evt);
         }
      });
      passwordTextField.addKeyListener(new java.awt.event.KeyAdapter()
      {
         public void keyReleased(java.awt.event.KeyEvent evt)
         {
            passwordTextFieldKeyReleased(evt);
         }
      });

      loginButton.setMnemonic('l');
      loginButton.setText("Login");
      loginButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            loginButtonActionPerformed(evt);
         }
      });
      loginButton.addKeyListener(new java.awt.event.KeyAdapter()
      {
         public void keyReleased(java.awt.event.KeyEvent evt)
         {
            loginButtonKeyReleased(evt);
         }
      });

      cancelButton.setMnemonic('c');
      cancelButton.setText("Cancel");
      cancelButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            cancelButtonActionPerformed(evt);
         }
      });

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
               .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
               .add(usernameTextField)
               .add(passwordTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addContainerGap(34, Short.MAX_VALUE)
            .add(loginButton)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(cancelButton)
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(jLabel1)
               .add(usernameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(passwordTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(cancelButton)
               .add(loginButton))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void loginButtonKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_loginButtonKeyReleased
   {//GEN-HEADEREND:event_loginButtonKeyReleased
      if (evt.getKeyCode() == KeyEvent.VK_ENTER)
      {
         attemptLogin();
      }
   }//GEN-LAST:event_loginButtonKeyReleased

   private void passwordTextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_passwordTextFieldFocusGained
   {//GEN-HEADEREND:event_passwordTextFieldFocusGained
      highlightAllText(passwordTextField);
   }//GEN-LAST:event_passwordTextFieldFocusGained

   private void usernameTextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_usernameTextFieldFocusGained
   {//GEN-HEADEREND:event_usernameTextFieldFocusGained
      highlightAllText(usernameTextField);
   }//GEN-LAST:event_usernameTextFieldFocusGained

   private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
   {//GEN-HEADEREND:event_cancelButtonActionPerformed
      WorkTimer.fireUserChanged();
      dispose();
   }//GEN-LAST:event_cancelButtonActionPerformed

   private void loginButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loginButtonActionPerformed
   {//GEN-HEADEREND:event_loginButtonActionPerformed
      attemptLogin();
   }//GEN-LAST:event_loginButtonActionPerformed

   private void passwordTextFieldKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_passwordTextFieldKeyReleased
   {//GEN-HEADEREND:event_passwordTextFieldKeyReleased
      if (evt.getKeyCode() == KeyEvent.VK_ENTER)
      {
         attemptLogin();
      }
   }//GEN-LAST:event_passwordTextFieldKeyReleased

   private void usernameTextFieldKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_usernameTextFieldKeyReleased
   {//GEN-HEADEREND:event_usernameTextFieldKeyReleased
      if (evt.getKeyCode() == KeyEvent.VK_ENTER)
      {
         attemptLogin();
      }
   }//GEN-LAST:event_usernameTextFieldKeyReleased
   
   /**
    * @param args the command line arguments
    */
   public static void main(String args[])
   {
      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            new Authorize().setVisible(true);
         }
      });
   }
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton cancelButton;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JButton loginButton;
   private javax.swing.JPasswordField passwordTextField;
   private javax.swing.JTextField usernameTextField;
   // End of variables declaration//GEN-END:variables
   
}
